package com.reflex;

import java.lang.reflect.Constructor;
import java.lang.reflect.InvocationTargetException;
//获取指定的构造器getConstructor
public class ReflexDemo03 {
    public static void main(String[] args) throws ClassNotFoundException, NoSuchMethodException, InvocationTargetException, InstantiationException, IllegalAccessException {
        // 创建对象
        Person person = new Person("ls");
        System.out.println(person);


        // 获取Person的Class对象
        Class<?> aClass = Class.forName("com.reflex.Person");
        // 获取无参构造方法
        //Constructor<?> constructor = aClass.getConstructor();
        // 获取有参构造方法
        Constructor<?> constructor = aClass.getConstructor(String.class);
        // 通过有参构造方法创建对象
        Object object = constructor.newInstance("zs");
        System.out.println(object);

        // 获取全参构造方法
        Constructor<?> constructor1 = aClass.getConstructor(String.class, int.class);
        // 通过全参构造方法创建对象
        Object object1 = constructor1.newInstance("ww", 18);
        System.out.println(object1);
    }
}
